/**
 * Created by 55456 on 2016/5/15.
 */
var auth = require("./auth.js");
var util = require("util");
var account = "test";
var password = "test";
var user = {
    type:"admin",
    account:"test",
    name:"oureda",
    pwd:"test"
};

var crypto = require("crypto");

module.exports = (app) => {

    app.post("/user/login",function(req,res){
        req.checkBody("username").notEmpty();
        req.checkBody("pwd").notEmpty();
        if(req.validationErrors()){
            res.send({
                status:0,
                msg:"username and pwd should not empty"
            });
            return;
        }
        var body = req.body;
        if(body.username === account || body.pwd === password){
            var token = crypto
                .createHmac("sha1",account)
                .digest("base64");
            auth.login(token,user);
            res.send({
                status:1,
                token:token
            });
            return;
        }
        res.send({
            status:0,
            msg:"username or pwd is error"
        });
    });

    app.post("/user/register",function(req,res){
        req.checkBody("username").notEmpty();
        req.checkBody("pwd").notEmpty();
        if(req.validationErrors()){
            res.send("username and pwd should not empty");
            return;
        }
        var body = req.body;
        //store in database
        res.send("ok");
    });
    app.post("/user/test",function(req,res){
        var user = auth.user(req);
        console.log(user);
        user = JSON.parse(user);
        // if(util.isNullorUndefined(user) || user.type !== "admin"){
        //     //do ..
        //     return;
        // }
        res.send(user);
    });
};
